using System;
using GTA;

namespace CustomPeds;

public class VehicleSexScript : Script
{
	public enum Status
	{
		None,
		Sex
	}

	public enum SexType
	{
		None,
		SexOralSit,
		SexSit
	}

	private static SexType sexType;

	private static Status status;

	private static float sexSpeed = 1f;

	private static int animIndex;

	private static readonly string[,] SexSitAnimMale = new string[4, 2]
	{
		{ "mini@prostitutes@sexnorm_veh", "sex_loop_male" },
		{ "mini@prostitutes@sexnorm_veh_first_person", "sex_loop_male" },
		{ "mini@prostitutes@sexlow_veh", "low_car_sex_loop_player" },
		{ "mini@prostitutes@sexlow_veh_first_person", "low_car_sex_loop_player" }
	};

	private static readonly string[,] SexSitAnimFemale = new string[4, 2]
	{
		{ "mini@prostitutes@sexnorm_veh", "sex_loop_prostitute" },
		{ "mini@prostitutes@sexnorm_veh_first_person", "sex_loop_prostitute" },
		{ "mini@prostitutes@sexlow_veh", "low_car_sex_loop_female" },
		{ "mini@prostitutes@sexlow_veh_first_person", "low_car_sex_loop_female" }
	};

	private static readonly string[,] SexOralSitAnimMale = new string[4, 2]
	{
		{ "mini@prostitutes@sexnorm_veh", "bj_loop_male" },
		{ "mini@prostitutes@sexnorm_veh_first_person", "bj_loop_male" },
		{ "mini@prostitutes@sexlow_veh", "low_car_bj_loop_player" },
		{ "mini@prostitutes@sexlow_veh_first_person", "low_car_bj_loop_player" }
	};

	private static readonly string[,] SexOralSitAnimFemale = new string[4, 2]
	{
		{ "mini@prostitutes@sexnorm_veh", "bj_loop_prostitute" },
		{ "mini@prostitutes@sexnorm_veh_first_person", "bj_loop_prostitute" },
		{ "mini@prostitutes@sexlow_veh", "low_car_bj_loop_female" },
		{ "mini@prostitutes@sexlow_veh_first_person", "low_car_bj_loop_female" }
	};

	public VehicleSexScript()
	{
		Tick += OnTick;
		// 优化：设置合理的脚本间隔
		Interval = 0;
	}

	private void OnTick(object sender, EventArgs e)
	{
		if (Main.m_Status != Main.MaleStatus.Vehicle || status != Status.Sex)
		{
			return;
		}
		
		try
		{
			OtherHelper.DisplayHelpTextThisFrame($"~INPUT_CELLPHONE_LEFT~ ~INPUT_CELLPHONE_RIGHT~ {TextSetting.vhc_HelpText_Rythm}~n~" + $"~INPUT_SPRINT~ {TextSetting.vhc_HelpText_Anim}~n~" + $"~INPUT_CONTEXT~ {TextSetting.vhc_HelpText_Exit}");
			
			if (Game.IsControlJustPressed(0, Control.Context))
			{
				sexSpeed = 0.55f;
				if (Main.P1.Character != null && Main.P1.Character.IsAlive)
				{
					Main.P1.Character.PlayAmbientSpeech1("SEX_CLIMAX", immediately: true);
				}
				if (Main.P2.Character != null && Main.P2.Character.IsAlive)
				{
					Main.P2.Character.PlayAmbientSpeech2("SEX_FINISHED", immediately: true);
				}
				SexSpeedChanges();
				
				if (Main.P2.Character != null && Main.P2.Character.IsAlive)
				{
					Main.P2.Character.IsSpeechPlayingToEnd();
				}
				if (Main.P1.Character != null && Main.P1.Character.IsAlive)
				{
					Main.P1.Character.IsSpeechPlayingToEnd();
				}
				
				StageEnd();
			}
			
			if (Game.IsControlJustPressed(0, Control.PhoneLeft))
			{
				if (sexSpeed >= 0.55f)
				{
					sexSpeed -= 0.05f;
				}
				else
				{
					sexSpeed = 0.55f;
				}
				SexSpeedChanges();
			}
			
			if (Game.IsControlJustPressed(0, Control.PhoneRight))
			{
				if (sexSpeed <= 2f)
				{
					sexSpeed += 0.05f;
				}
				else
				{
					sexSpeed = 2f;
				}
				SexSpeedChanges();
			}
			
			if (Game.IsControlJustPressed(0, Control.Sprint))
			{
				animIndex++;
				if (animIndex >= 4)
				{
					animIndex = 0;
				}
				sexSpeed = 1f;
				
				if (sexType == SexType.SexOralSit)
				{
					if (Main.P1.Character != null && Main.P1.Character.IsAlive)
					{
						Main.P1.Character.TaskPlayAnimLoop(SexOralSitAnimMale[animIndex, 0], SexOralSitAnimMale[animIndex, 1], -1);
					}
					if (Main.P2.Character != null && Main.P2.Character.IsAlive)
					{
						Main.P2.Character.TaskPlayAnimLoop(SexOralSitAnimFemale[animIndex, 0], SexOralSitAnimFemale[animIndex, 1], -1);
					}
				}
				
				if (sexType == SexType.SexSit)
				{
					if (Main.P1.Character != null && Main.P1.Character.IsAlive)
					{
						Main.P1.Character.TaskPlayAnimLoop(SexSitAnimMale[animIndex, 0], SexSitAnimMale[animIndex, 1], -1);
					}
					if (Main.P2.Character != null && Main.P2.Character.IsAlive)
					{
						Main.P2.Character.TaskPlayAnimLoop(SexSitAnimFemale[animIndex, 0], SexSitAnimFemale[animIndex, 1], -1);
					}
				}
			}
		}
		catch (Exception ex)
		{
			UI.Notify($"车辆性爱脚本错误: {ex.Message}");
		}
	}

	public static void InitSexPlay(SexType _sexType)
	{
		try
		{
			sexType = _sexType;
			status = Status.Sex;
			sexSpeed = 1f;
			animIndex = 0;
			
			if (sexType == SexType.SexOralSit)
			{
				if (Main.P2.Character != null && Main.P2.Character.IsAlive)
				{
					Main.P2.Character.Task.LookAt(Main.P1.Character);
					Main.P2.Character.TaskPlayAnimLoop("mini@prostitutes@sexnorm_veh", "bj_loop_prostitute", -1);
					Main.P2.Character.PlayAmbientSpeech2("SEX_ORAL_FEM", immediately: true);
				}
				if (Main.P1.Character != null && Main.P1.Character.IsAlive)
				{
					Main.P1.Character.TaskPlayAnimLoop("mini@prostitutes@sexnorm_veh", "bj_loop_male", -1);
					Main.P1.Character.PlayAmbientSpeech1("SEX_GENERIC", immediately: true);
				}
			}
			
			if (sexType == SexType.SexSit)
			{
				if (Main.P2.Character != null && Main.P2.Character.IsAlive)
				{
					Main.P2.Character.Task.LookAt(Main.P1.Character);
					Main.P2.Character.TaskPlayAnimLoop("mini@prostitutes@sexnorm_veh", "sex_loop_prostitute", -1);
					Main.P2.Character.PlayAmbientSpeech2("SEX_GENERIC", immediately: true);
				}
				if (Main.P1.Character != null && Main.P1.Character.IsAlive)
				{
					Main.P1.Character.TaskPlayAnimLoop("mini@prostitutes@sexnorm_veh", "sex_loop_male", -1);
					Main.P1.Character.PlayAmbientSpeech1("SEX_GENERIC", immediately: true);
				}
			}
		}
		catch (Exception ex)
		{
			UI.Notify($"初始化车辆性爱失败: {ex.Message}");
		}
	}

	private void SexSpeedChanges()
	{
		try
		{
			if (sexType == SexType.SexOralSit)
			{
				if (Main.P1.Character != null && Main.P1.Character.IsAlive)
				{
					Main.P1.Character.SetEntityAnimCurrentSpeed(SexOralSitAnimMale[animIndex, 0], SexOralSitAnimMale[animIndex, 1], sexSpeed);
				}
				if (Main.P2.Character != null && Main.P2.Character.IsAlive)
				{
					Main.P2.Character.SetEntityAnimCurrentSpeed(SexOralSitAnimFemale[animIndex, 0], SexOralSitAnimFemale[animIndex, 1], sexSpeed);
					Main.P2.Character.PlayAmbientSpeech2("SEX_ORAL_FEM", immediately: false);
				}
			}
			
			if (sexType == SexType.SexSit)
			{
				if (Main.P1.Character != null && Main.P1.Character.IsAlive)
				{
					Main.P1.Character.SetEntityAnimCurrentSpeed(SexSitAnimMale[animIndex, 0], SexSitAnimMale[animIndex, 1], sexSpeed);
				}
				if (Main.P2.Character != null && Main.P2.Character.IsAlive)
				{
					Main.P2.Character.SetEntityAnimCurrentSpeed(SexSitAnimFemale[animIndex, 0], SexSitAnimFemale[animIndex, 1], sexSpeed);
					Main.P2.Character.PlayAmbientSpeech2("SEX_GENERIC", immediately: false);
				}
			}
		}
		catch (Exception ex)
		{
			LogHelper.WriteException(ex, "SexSpeedChanges - 车辆性爱速度变化");
		}
	}

	public static void StageEnd()
	{
		try
		{
			if (Main.P2.Character != null && Main.P2.Character.IsAlive)
			{
				Main.P2.Character.Task.ClearAll();
			}
			if (Main.P1.Character != null && Main.P1.Character.IsAlive)
			{
				Main.P1.Character.Task.ClearAll();
			}
			
			sexType = SexType.None;
			status = Status.None;
			Main.m_Status = Main.MaleStatus.None;
			
			if (Main.P2.Character != null && Main.P2.Character.IsDead)
			{
				Main.f_Status = Main.FemaleStatus.None;
			}
			
			if (Main.P1.skin.Voice != string.Empty && Main.P1.skin.Voice != null)
			{
				Main.P1.Character.SetVoice(Main.P1.skin.Voice);
			}
			if (Main.P2.skin.Voice != string.Empty && Main.P2.skin.Voice != null)
			{
				Main.P2.Character.SetVoice(Main.P2.skin.Voice);
			}
		}
		catch (Exception ex)
		{
			UI.Notify($"结束车辆性爱失败: {ex.Message}");
		}
	}
}
